import time
from . import ConnPG
import psycopg2
from sanic.log import logger

class SpuTable:

    async def add_spu(self, spu_name, class_id):#添加spu

        #参数检查
        if not (class_id and spu_name):

            return {
                'status': -1,
                'errorCode': 108100,
                'errorMsg': '参数不能为空，该接口有两个必传参数',
                'resultBody': None
            }

        sql = 'INSERT INTO spu(spu_name, class_id) VALUES (%s, %s)'

        try:
            await ConnPG().execute(sql, (spu_name, class_id))
        except psycopg2.errors.UniqueViolation:

            return {

                'status': -1,
                'errorCode': 108101,
                'errorMsg': '当前spu名称已经存在',
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "SPU添加成功",
            }
    async def select_all_spu(self):#查看所有spu

        sql = 'select * from spu'

        result = await ConnPG().fetchall(sql)

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_all_spu_by_class_id(self, class_id):#查看指定类别的spu

        #参数检查
        if class_id == None or not class_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 108100,
                'errorMsg': '参数不能为空，该接口有一个必传参数且为整数',
                'resultBody': None
            }

        sql = 'select * from spu where class_id=%s'

        result = await ConnPG().fetchall(sql, (class_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    
    async def del_spu(self, spu_id):#删除spu

        #参数检查

        if not spu_id or not spu_id.isdecimal:

            return {
                'status': -1,
                'errorCode': 108100,
                'errorMsg': 'spu_id不能为空且为整数类型',
                'resultBody': None
            }

        sql = 'DELETE FROM spu WHERE id=%s'

        try:
            await ConnPG().execute(sql, (spu_id,))
        except psycopg2.errors.ForeignKeyViolation as e:

            # print(e)

            return {

                'status': -1,
                'errorCode': 108112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "spu删除成功",                                     
            }

    async def update_spu(self, spu_id, spu_name):#更新库房名称

        #参数检查
        if not all([spu_id, spu_name]) or not spu_id.isdecimal():


            return {
                'status': -1,
                'errorCode': 108100,
                'errorMsg': '接口有两个必传参数且spu_id为整数',
                'resultBody': None
            }

        sql = "UPDATE spu SET spu_name=%s WHERE id=%s"


        await ConnPG().execute(sql, (spu_name,spu_id))

    
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "spu修改成功",
        }

